package com.imohoo.libs.retrofit2;

import com.imohoo.libs.retrofit2.converter.SpJsonConverterFactory;
import com.imohoo.libs.retrofit2.ssl.CertUtils;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

import javax.net.ssl.TrustManagerFactory;
import java.util.concurrent.TimeUnit;

public class SpRetrofitManager {
    private static Retrofit retrofit;

    public static Retrofit getRetrofit() {
        try {
            if (retrofit == null) {
                TrustManagerFactory trustManagerFactory = CertUtils.getTrustManagerFactory();
                OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
                        .sslSocketFactory(CertUtils.getSocketFactory(trustManagerFactory), CertUtils.getTrustManager(trustManagerFactory))
                        .hostnameVerifier(CertUtils.getHostnameVerifier(new String[]{"*.myrunners.com"}))
                        .connectTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30, TimeUnit.SECONDS)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .build();

                retrofit = new Retrofit.Builder()
                        .client(mOkHttpClient)
                        .baseUrl("https://dev-api.myrunners.com/")
                        .addConverterFactory(SpJsonConverterFactory.create())
                        .build();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return retrofit;
    }
}